package Test;
import java.util.Scanner;
import java.util.*;
public class test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //获取数组长度
        int n = scanner.nextInt();
        //读取数组元素
        int[] arryr = new int[n];
        for (int i = 0; i < n; i++) {
            arryr[i] = scanner.nextInt();
        }
        //动态规划数组,用于存储最大分数
        int[] dp = new int[10001];
        //遍历数组，用于存储最大分数
        for (int num  : arryr) {
            dp[num] = Math.max(dp[num],dp[num-2]+num);
            dp[num + 1] = Math.max(dp[num + 1],dp[num] - num );
            dp[num + 2] = Math.max(dp[num+2],dp[num] -num );
        }
        //输出最大分数
        System.out.println(dp[10000]);
    }
}
